﻿//7. Write a program to convert from any numeral system of given base s to any other numeral system of base d (2 ≤ s, d ≤  16).


using System;
using System.Collections.Generic;

class ConvertFromAnyToAny
{
    static void Main()
    {
        string fromString = Console.ReadLine();
        int sBase = int.Parse(Console.ReadLine());
        int dBase = int.Parse(Console.ReadLine());
        Console.WriteLine(FromDecimalToString(FromStringToDecimal(fromString, sBase), dBase));
    }

    static int GetNumber(string fromString, int i)
    {
        int current = 0;

        if (fromString[i] - '0' > 9)
        {
            current = (fromString[i] - 'A') + 10;
        }
        else
        {
            current = fromString[i] - '0';
        }
        return current;
    }

    static char GetChar(int remainder)
    {
        char current = '0';

        if (remainder > 9)
        {
            current = Convert.ToChar((Convert.ToInt32('A') + remainder - 10).ToString());
        }
        else
        {
            current = Convert.ToChar(remainder.ToString());
        }
        return current;
    }

    static int FromStringToDecimal(string fromString, int sBase)
    {
        int decimalNumber = 0;

        for (int i = 0; i < fromString.Length; i++)
        {
            decimalNumber += GetNumber(fromString, i) * (int)Math.Pow(sBase, fromString.Length - 1 - i);
        }
        return decimalNumber;
    }

    static string FromDecimalToString(int decimalNumber, int dBase)
    {
        string result = String.Empty;
        int remainder = 0;

        while (decimalNumber > 0)
        {
            remainder = decimalNumber % dBase;
            decimalNumber /= dBase;

            result = GetChar(remainder) + result;
        }
        return result;
    }
}
